package com.zidongxiangxi.clock.server.controller;

import com.zidongxiangxi.clock.http.request.CreateClockRequest;
import com.zidongxiangxi.clock.http.request.DeleteClockRequest;
import com.zidongxiangxi.clock.server.business.ClockBusiness;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 闹钟http接口
 *
 * @author chenxudong
 * @date 2021/01/18
 */
@RestController
@RequestMapping("/clocks")
@RequiredArgsConstructor
public class ClockController {
    private final ClockBusiness clockBusiness;

    /**
     * 创建闹钟
     *
     * @param request 入参
     * @return 闹钟id
     */
    @PutMapping(value = "",
        consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Long createClock(@RequestBody CreateClockRequest request) {
        return clockBusiness.createClock(request.getApplication(), request.getBusinessId(), request.getPayload(),
            request.getTargetRingAt(), request.getLatestRingAt());
    }

    /**
     * 删除闹钟
     *
     * @param request 入参
     */
    @DeleteMapping(value = "",
        consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void deleteClock(@RequestBody DeleteClockRequest request) {
        clockBusiness.deleteClock(request.getApplication(), request.getBusinessId());
    }

    /**
     * 根据id删除闹钟
     *
     * @param clockId 闹钟id
     */
    @DeleteMapping("/{clockId}")
    public void deleteClockById(@PathVariable Long clockId) {
        clockBusiness.deleteClockById(clockId);
    }

}
